<?php

namespace app\admin\service;

use app\admin\dao\TransactionDao;
use app\admin\model\UM as UM;
use app\common\model\Role;
use ShidianUtil\Utils\StringUtil;
use think\exception\ValidateException;

/**
 * Class TransactionStatisticsService
 * @package app\admin\service
 */
class TransactionStatisticsService
{
    // dao
    private TransactionDao $transactionDao;

    public function __construct(TransactionDao $transactionDao)
    {
        $this->transactionDao = $transactionDao;
    }

    /**
     *
     * @param UM|null $user
     * @param $type
     * @param $companyId
     * @return array
     */
    public function read(UM $user, $type, $companyId, $startTime, $endTime): array
    {
        if (StringUtil::equals($type, 'company')) {
            if (StringUtil::equals($user->roleType, Role::ROLE_TYPE_COMPANY)) {
                $companyId = $user->id;
            }

            return $this->transactionDao->statisticsListTransactionOfCompany($companyId, $startTime, $endTime);
        }

        if (StringUtil::equals($type, 'member')) {
            if (StringUtil::equals($user->roleType, Role::ROLE_TYPE_COMPANY)) {
                throw new ValidateException('您不能查看用户充值数据');
            }

            return $this->transactionDao->statisticsListTransactionOfMember($startTime, $endTime);
        }

        throw new ValidateException('请选择公司或用户数据');
    }
}
